import React, { useEffect, useState } from 'react';
import { AutoComplete, Form, message, Checkbox, Row, Col, } from 'antd';
// import { DICT_TYPE, getOptions } from '@/global/service';
import ProForm, {
  ModalForm,
  ProFormText,
  ProFormCheckbox,
  ProFormSelect,
  ProFormRadio,
  ProFormDateTimePicker,
  ProFormDigit
} from '@ant-design/pro-form';
import { history } from 'umi';
// import {
//   MANUAL_PUSH,
//   ,
//   JUMPWAY_MODULE,
//   AUDIENCETYPE_ALL,
//   ,
//   AUDIENCETYPE_CLASS,
// } from '../../constants';
import { sendBatch, getValueAndTextList, getGroupListByAid } from './service';
import moment from 'moment';

declare const ValidateStatuses: ['success', 'warning', 'error', 'validating', '', true];
declare type ValidateStatus = typeof ValidateStatuses[number];

interface AddFormProps {
  modalVisible: boolean;
  // modalType:string;
  paperId:number;
  activityId:number;
  onCancel: () => void;
  onReload: () => void;
}

const requireStyle = {
  color: '#ff4d4f',
  marginRight: '4px',
  fontSize: '14px',
  fontFamily: 'SimSun, sans-serif',
};

const Single: React.FC<AddFormProps> = (props) => {
  const [form] = Form.useForm();
  const { modalVisible, paperId, activityId, onCancel, onReload } = props;
  const [showData, setShowData] = useState(true);
  const [options, setOptions] = useState([]);
  const [receiverId, setReceiverId] = useState(null);
  const [name, setName] = useState(null);

  const [batchValidStatus, setBatchValidStatus] = useState<any>('');
  const [batchErrorText, setBatchErrorText] = useState<string>('');

  const [alistValidStatus, setAlistValidStatus] = useState<any>('');
  const [alistErrorText, setAlistErrorText] = useState<string>('');

  const [validateTotalStatus, setValidateTotalStatus] = useState<any>('');

  const batchStatusHanlder = (status: ValidateStatus, text: string) => {
    setBatchValidStatus(status);
    setBatchErrorText(text);
  };
 
  const onFinish = async function () {
    const data = {}
    const values = form.getFieldsValue();
    data.validTime = moment(new Date(values.startTime)).format('YYYY-MM-DD HH:mm:00');
    data.paperId = paperId;
    data.activityId = activityId;
    data.groupId= values.groupIds;
    data.studentTagIds= values.studentTagIds
    // data.studentTagIds= values.studentTagIds.join(',');
    console.log(data)
  
    // return
    const res = await sendBatch(data);
    if (res.code === 200) {
      form.resetFields();
      
      onReload();
      onCancel();
      message.success('添加成功');
    } else {
      if (res.message.startsWith('500')) {
        batchStatusHanlder('error', res.message.replace('500', ''));
        return;
      }
      message.error(res.message);
    }
  };
  const getTag = async () => {
    const data = await getValueAndTextList({code:'level_tag'});
    const optionList = data.result.map((item: { value: any; des: any }) => ({
      label: item.des,
      value: item.value,
      // time: item.starttime,
    }));
    return optionList;
    // setOptions(optionList);
  };

  const getGroup = async () => {
    const data = await getGroupListByAid({activityId});
    const optionList = data.result.map((item: { id: any; name: any }) => ({
      label: item.name,
      value: item.id,
      // time: item.starttime,
    }));
    return optionList;
    // setOptions(optionList);
  };
  
  useEffect(() => {
    if (batchValidStatus === 'error' || alistValidStatus === 'error') {
      setValidateTotalStatus('error');
    } else {
      setValidateTotalStatus('');
    }
  }, [batchValidStatus, alistValidStatus]);
  return (
    <ModalForm
      title="批量发放"
      visible={modalVisible}
      form={form}
      onFinish={onFinish}
      modalProps={{
        onCancel: () => {
          form.resetFields();

          onCancel();
        },
      }}
    >
      <ProForm.Item>
      <ProFormSelect
        label="分组名称"
        name="groupIds"
        // mode="multiple"
        key="分组名称"
        request={getGroup}
        rules={[
          {
            required: true,
            message: '请选择班级',
          },
        ]}
      />

      </ProForm.Item>
      
       <ProForm.Item>
       <ProFormSelect
        label="学员标签"
        key="学员标签"
        name="studentTagIds"
        mode="multiple"
        request={getTag}
        placeholder="不选为全部"
        // rules={[
        //   {
        //     required: true,
        //     message: '不选为全部',
        //   },
        // ]}
      />

       </ProForm.Item>
      
      
      
      <ProFormDateTimePicker
        label="生效时间"
        name="startTime"
        width="md"
        fieldProps={{
          format: 'YYYY-MM-DD HH:mm:00',
        }}
        placeholder="请选择生效时间"
        rules={[
          {
            required: true,
            message: '请选择生效时间',
          }
        ]}
      />
    </ModalForm>
  );
};

export default Single;
